package org.kongqian.user.controller;

import org.kongqian.basic.utils.AjaxResult;
import org.kongqian.user.dto.VerifyCodeDTO;
import org.kongqian.user.service.IVerifyCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/img/{key}")
    public AjaxResult getImageVerifyCode(@PathVariable("key") String key){
        try {
            String base64ImageCode=verifyCodeService.getImageVerifyCode(key);
            return AjaxResult.getResult().setObject(base64ImageCode);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @PostMapping("/sendSmsCode")
    public AjaxResult sendSmsCode(@RequestBody VerifyCodeDTO dto){
        try {
            verifyCodeService.sendSmsCode(dto);
            return AjaxResult.getResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @PostMapping("/wechatBind/phoneCode")
    public AjaxResult wechatBindSendSmsCode(@RequestBody VerifyCodeDTO dto){
        try {
            verifyCodeService.wechatBindSendSmsCode(dto);
            return AjaxResult.getResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage(e.getMessage());
        }
    }
}
